/**
 * LOCALSTORAGE 公共存储方法
 * @param [Array] data [传入的JSON数组 对象]
 * @param [string] name [要存取的JSON数组 名称]
 * @returns [Object] 解析后的内容
 */

export default {
  storageSet(data, name) {
    let names = name.toUpperCase(),
      key = '__' + names + '__';
    if (typeof data == 'string') {
      localStorage[key] = data;
    } else {
      let datas;
      try {
        datas = JSON.stringify(data);
      } catch (e) {
        datas = '';
      }
      if (datas) {
        if (datas.length > 2 && typeof data !== undefined) {
          localStorage[key] = JSON.stringify(data);
        }
      }
    }
  },

  storageGet(name) {
    let names = name.toUpperCase(),
      key = '__' + names + '__',
      data;
    try {
      data = JSON.parse(localStorage[key]);
    } catch (e) {
      data = localStorage[key];
    }
    if (localStorage[key] !== undefined)
      return data;
  },

  storageRemove(name) {
    let names = name.toUpperCase(),
      key = '__' + names + '__';
    if (localStorage[key]) localStorage.removeItem(key);
  },
  storageClear() {
    localStorage.clear();
  }
}
